﻿<?php
/**  
* PrtSupDAO.php
*  검색log기록 남기기
*  복수의 키워드를 단일 테이블에 적용하는 경우 : SELECT * FROM `list_supplies` WHERE sSerial IN ('s00004', 's00259', 's01100') 형태로 쿼리
*   
*/

require_once(CLASS_PATH."/__autoload.php");

class PrtSupDAO extends Searchmgr
{
	protected $arrStr = array(), $q;
	
	protected function getQuery($keywords)
	{
		if(preg_match('/^p\d{5}/i', $keywords[0])) 
		{
			$table 		 = ' list_link AS ll, list_supplies AS ls ';	
			$whereClause = " (ll.pSerial LIKE '%$keywords[0]%') AND (ls.sSerial = ll.sSerial) "; 
			$fields = 'ls.sSerial, ls.sMaker, ls.sName, ls.sColor, ls.sUnit, ls.sQuantity, ls.sType, ls.eco, ls.short, ls.sComment ';
			$order  = 'ls.sMaker, ls.sType DESC, ls.sColor, ls.sName ';
			$qry 	= $this->db_select($table, $whereClause, $fields, $order);
			
			
		    $q = "SELECT sSerial FROM list_link WHERE (pSerial = '$keywords[0]')";			
			$q = $this->db_query($q);
			
			while($row = mysqli_fetch_array($q)) 
			{
			  $arrStr[] = $row['sSerial'];				
			}
			
			if(empty($arrStr)) return false;
			
			$q = implode(', ', $arrStr);
			$this->recSearchLog($q, 's');	// 검색log기록 남기기
			
			return $qry;  	
		}		
		else
		{
/*			
			$keywords = implode('", "', $keywords);				
			$fields = ' sSerial, sMaker, sName, sColor, sUnit, sQuantity, sType, eco, short, sComment ';
			$order  = ' sMaker, sType DESC, sColor, sName ';	
			$qry 	= "SELECT $fields FROM list_supplies  WHERE sSerial IN (\"$keywords\") ORDER BY $order";
			
/**/			 

     		$log_keywords = implode('", "', $keywords);	
			$this->recSearchLog($log_keywords, 's');	// 검색log기록 남기기	

			$keywords = implode('") OR (hs.sSerial LIKE  "', $keywords);	
			$keywords = '((hs.sSerial LIKE  "'.$keywords.'"))';
			
			$table  = ' list_Supplies AS ls, haystack_Supplies AS hs ';
			$fields = ' ls.sSerial, ls.sMaker, ls.sName, ls.sColor, ls.sUnit, ls.sQuantity, ls.sType, eco, ls.short, ls.sComment, hs.spSerial ';			
			$order  = ' ls.sMaker, ls.sType DESC, ls.sColor, ls.sName ';	
			
			$qry    = "SELECT $fields FROM $table  WHERE $keywords  AND (ls.sSerial = hs.sSerial) ORDER BY $order";			
			return $this->db_query($qry);  	
		}
	    
	}
   
}

?>
